$(function(){
	$("#sendBtn").click(send_letter);
	$(".delete_msg").click(delete_msg);
});

function send_letter() {
	var toUsername = $("#recipient-name").val();
	var content = $("#message-text").val();
	$.post(
	    CONTEXT_PATH + "/letter/add",
	    {"toUsername": toUsername, "content": content},
	    function(resp){
	        var data = $.parseJSON(resp);
	        $("#hintBody").text(data.msg);
	        $("#hintModal").modal("show");
            setTimeout(function(){
                $("#hintModal").modal("hide");
                if (data.code == 200){
                    $("#sendModal").modal("hide");
                    location.reload();
                }
            }, 2000);
	    }
	);
}

function delete_msg() {
	// TODO 删除数据
    var messageId = $(this).attr("id");
    $.post(
        CONTEXT_PATH + "/letter/delete",
        {"messageId": messageId},
        function(resp){
            var data = $.parseJSON(resp);
            $("#hintBody").text(data.msg);
            $("#hintModal").modal("show");
            setTimeout(function(){
                $("#hintModal").modal("hide");
                if (data.code == 200){
                    $("#sendModal").modal("hide");
                    $(this).parents(".media").remove();
                    location.reload();
                }
            }, 2000);
        }
    )
}